<?php

namespace frontend\modules\user\controllers;

use Yii;
use frontend\components\Controller;
use yii\helpers\Inflector;
use frontend\modules\user\models\AddressForm;
use common\models\user\UserAddress;
use common\logics\UserAddressLogic;
use common\helpers\Url;

/**
 * 用户收件地址相关接口
 * AddressController for the `user` module
 */
class AddressController extends Controller {

    public $modelClass = 'frontend\modules\user\models\AddressSearch';

    /**
     * @inheritdoc
     */
    protected function rules() {
        return [
            [
                'allow' => true,
                'roles' => ['@'],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    protected function verbs() {
        return [
            'index' => ['GET'],
            'view' => ['GET'],
            'delete' => ['DELETE'],
            '*' => ['POST'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions() {
        return [
            'view' => [
                'class' => 'yii\rest\ViewAction',
                'modelClass' => $this->modelClass,
            ],
        ];
    }

    /**
     * 地址列表
     * @return mixed
     */
    public function actionIndex() {
        $address = UserAddressLogic::findAll(['user_id' => $this->uid]);
        $addlink = Url::weblink('user/address/add');
        return compact('address', 'addlink');
    }

    /**
     * 
     * @return mixed
     */
//    public function actionView($id) {
//        return UserAddressLogic::findOne([
//            'id' => $id,
//            'user_id' => $this->token->user_id
//        ]);
//    }

    /**
     * 新增收件地址
     * @return mixed
     */
    public function actionCreate() {
        $model = new AddressForm();
        $params = $this->formatPost(Yii::$app->request->post());
        if (!$model->load($params, '')) {
            parent::warning('数据未加载成功');
        }
        $model->user_id = $this->token->user_id;
        return $model->save();
    }

    /**
     * 修改收件地址
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = new AddressForm($id);
        $params = $this->formatPost(Yii::$app->request->post());
        if (!$model->load($params, '')) {
            parent::warning('数据未加载成功');
        }
        $model->user_id = $this->token->user_id;
        return $model->save();
    }

    /**
     * 删除收件地址
     * @return mixed
     */
    public function actionDelete($id) {
        $model = UserAddress::findOne([
            'id' => $id,
            'user_id' => $this->token->user_id
        ]);
        if (!$model) {
            return true;
        }
        return $model->delete();
    }

    /**
     * 设置默认收件地址
     * @return mixed
     */
    public function actionDefault($id) {
        $userid = $this->token->user_id;
        $model = UserAddress::findOne([
            'id' => $id,
            'user_id' => $userid
        ]);
        if (!$model) {
            return false;
        }
        $model->is_default = UserAddress::IS_DEFAULT_YES;
        if (!$model->save()) {
            return false;
        }
        UserAddress::updateAll(['is_default' => UserAddress::IS_DEFAULT_NO], [
            'and',
            ['user_id' => $userid],
            ['!=', 'id', $model->id]
        ]);
        return true;
    }

    protected function formatPost($post) {
        $params = [];
        foreach ($post as $key => $vls) {
            $name = Inflector::camel2id($key, '_');
            $params[$name] = $vls;
        }
        return $params;
    }

}
